package com.situ.stmall.manager.service.impl;

import com.situ.stmall.manager.entity.Admin;
import com.situ.stmall.manager.mapper.AdminMapper;
import com.situ.stmall.manager.service.AdminService;
import com.situ.stmall.manager.util.MD5Util;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@AllArgsConstructor  // 相当于下面的public AdminServiceImpl,没有会报空指针
public class AdminServiceImpl implements AdminService {

    /**
     * Spring4.X支持构造方法注入
     * 提供一个有参的构造方法，不需要任何注解
     */
    private AdminMapper adminMapper;
//    public AdminServiceImpl(AdminMapper adminMapper){
//        this.adminMapper = adminMapper;
//    }

    @Override
    public Admin login(Admin admin) throws Exception {
        //参数验证
        if(admin.getUsername() == null ||
           admin.getUsername().length()<3 ||
           admin.getUsername().length()>16){
            throw new Exception("用户名格式错误");
        }
        if(admin.getPassword() == null ||
           admin.getPassword().length() < 3 ||
           admin.getPassword().length() > 16){
            throw new Exception("密码格式错误");
        }
        Admin sAdmin = adminMapper.selectByUsername(admin.getUsername());
        if(sAdmin == null){
            throw new Exception("用户名不存在");
        }
       // MD5加密
        String password = admin.getPassword();
        password = MD5Util.getMD5(password+sAdmin.getSalt());
        System.out.println(password);
        if(! password.equals(sAdmin.getPassword())){
            throw new Exception("密码错误");
        }
        if(sAdmin.getStatus() == 1){
            throw new Exception("用户已禁用");
        }
        return sAdmin;
    }

    /**
     * 修改密码
     *
     * @param id
     * @param oldPassword
     * @param newPassword
     * @param rePasswords
     * @return
     * @throws Exception
     */
    @Override
    public Admin modifyPwd(Integer id, String oldPassword, String newPassword, String rePasswords) throws Exception {
        if(oldPassword == null || oldPassword.length() < 3 || oldPassword.length() > 16){
            throw new Exception("旧密码格式错误");
        }
        if(newPassword == null || newPassword.length() < 3 || newPassword.length() > 16){
            throw new Exception("新密码格式错误");
        }
        if(! newPassword.equals(oldPassword)){
            throw new Exception("两次密码不一致");
        }
        Admin admin = adminMapper.selectById(id);
        String md5Pwd = MD5Util.getMD5(oldPassword+admin.getSalt());
        if(! md5Pwd.equals(admin.getPassword())){
            throw new Exception("旧密码错误");
        }
        admin.setPassword(MD5Util.getMD5(newPassword+admin.getSalt()));
        adminMapper.update(admin);
        return admin;
    }

    @Override
    public Admin edit(Admin admin) throws Exception {
        if(admin.getPhone().length() > 11){
            throw new Exception("手机号长度不能超过11位");
        }
        if(admin.getEmail().length() > 64){
            throw new Exception("邮箱长度不能超过64位");
        }
        if(admin.getRealname().length() > 32){
            throw new Exception("真实姓名长度不能超过32位");
        }
        if(admin.getStatus() != 0 || admin.getStatus() != 1){
            throw new Exception("状态只能为0或1");
        }
        return admin;
    }
}
